package com.liy.framework.common.utils;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.Iterator;
import java.util.List;
import org.apache.tools.zip.ZipEntry;
import org.apache.tools.zip.ZipFile;
import org.apache.tools.zip.ZipOutputStream;

public class ZipUtil {
    private static byte[] _byte = new byte[1024];

    public ZipUtil() {
    }

    public static void zipFile(String zip, List<File> srcFiles) {
        try {
            if (!zip.endsWith(".zip") && !zip.endsWith(".ZIP")) {
                System.out.println("target file[" + zip + "] is not .zip type file");
            } else {
                ZipOutputStream _zipOut = new ZipOutputStream(new FileOutputStream(new File(zip)));
                _zipOut.setEncoding("GBK");
                Iterator var3 = srcFiles.iterator();

                while(var3.hasNext()) {
                    File _f = (File)var3.next();
                    handlerFile(zip, _zipOut, _f, "");
                }

                _zipOut.close();
            }
        } catch (FileNotFoundException var5) {
            ;
        } catch (IOException var6) {
            ;
        }

    }

    private static void handlerFile(String zip, ZipOutputStream zipOut, File srcFile, String path) throws IOException {
        System.out.println(" begin to compression file[" + srcFile.getName() + "]");
        if (!"".equals(path) && !path.endsWith(File.separator)) {
            path = path + File.separator;
        }

        if (!srcFile.getPath().equals(zip)) {
            if (srcFile.isDirectory()) {
                File[] _files = srcFile.listFiles();
                if (_files.length == 0) {
                    zipOut.putNextEntry(new ZipEntry(path + srcFile.getName() + File.separator));
                    zipOut.closeEntry();
                } else {
                    File[] var5 = _files;
                    int var6 = _files.length;

                    for(int var7 = 0; var7 < var6; ++var7) {
                        File _f = var5[var7];
                        handlerFile(zip, zipOut, _f, path + srcFile.getName());
                    }
                }
            } else {
                InputStream _in = new FileInputStream(srcFile);
                zipOut.putNextEntry(new ZipEntry(path + srcFile.getName()));
                boolean var10 = false;

                int len;
                while((len = _in.read(_byte)) > 0) {
                    zipOut.write(_byte, 0, len);
                }

                _in.close();
                zipOut.closeEntry();
            }
        }

    }

    public static List<File> upzipFile(String zipPath, String descDir) {
        return upzipFile(new File(zipPath), descDir);
    }

    public static List<File> upzipFile(File zipFile, String descDir) {
        ArrayList _list = new ArrayList();

        try {
            ZipFile _zipFile = new ZipFile(zipFile, "GBK");
            Enumeration entries = _zipFile.getEntries();

            while(true) {
                while(entries.hasMoreElements()) {
                    ZipEntry entry = (ZipEntry)entries.nextElement();
                    File _file = new File(descDir + File.separator + entry.getName());
                    if (entry.isDirectory()) {
                        _file.mkdirs();
                    } else {
                        File _parent = _file.getParentFile();
                        if (!_parent.exists()) {
                            _parent.mkdirs();
                        }

                        InputStream _in = _zipFile.getInputStream(entry);
                        OutputStream _out = new FileOutputStream(_file);
                        boolean var10 = false;

                        int len;
                        while((len = _in.read(_byte)) > 0) {
                            _out.write(_byte, 0, len);
                        }

                        _in.close();
                        _out.flush();
                        _out.close();
                        _list.add(_file);
                    }
                }

                return _list;
            }
        } catch (IOException var11) {
            return _list;
        }
    }

    public static void deletefile(String delpath) {
        try {
            File file = new File(delpath);
            if (!file.isDirectory()) {
                file.delete();
            } else if (file.isDirectory()) {
                String[] filelist = file.list();

                for(int i = 0; i < filelist.length; ++i) {
                    File delfile = new File(delpath + File.separator + filelist[i]);
                    if (!delfile.isDirectory()) {
                        delfile.delete();
                    } else if (delfile.isDirectory()) {
                        deletefile(delpath + File.separator + filelist[i]);
                    }
                }

                file.delete();
            }
        } catch (Exception var5) {
            var5.printStackTrace();
        }

    }

    public static void main(String[] args) {
        List<File> list = new ArrayList();
        list.add(new File("C:\\Users\\ChenA\\Documents\\简历"));
        zipFile("C:\\Users\\ChenA\\Documents\\简历\\jl.zip", list);
    }
}
